package com.project.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.project.interceptor.SysInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Created by 卢泓佚  414814103@qq.com on 2021/9/28
 */
@Component
public class SysConfig implements WebMvcConfigurer {
    @Autowired
    SysInterceptor sysInterceptor;

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //分页拦截器（如果不配置，IPage无法实现分页）
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    //此服务器允许的跨域访问
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")  //跨域的URL
                .allowedHeaders("*")  //请求头
                .allowedMethods("*")  //请求方法
                .allowedOrigins("*");  //请求域名
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //放行img路径下的所有
        registry.addResourceHandler("/img/**")
                //.addResourceLocations("file:d:/img/"); //window路径
                .addResourceLocations("file:/mytest/img/"); //linux路径
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(sysInterceptor)
                .addPathPatterns("/api/**");
    }
}
